package view;

import java.awt.*;

import javax.swing.*;

import model.EditorTool;
import model.LevelEditor;
import controller.EditorController;

/**
 * EditorToolbar provides a graphical user interface for setting tools
 * on the LevelEditor object.
 * @author Alex Dinardo
 *
 */

@SuppressWarnings("serial")
public class EditorToolbar extends JPanel {

	public EditorToolbar(LevelEditor editor, EditorController control)
	{
		super(new FlowLayout(FlowLayout.LEFT, 5, 0));
		Dimension buttonSize = new Dimension(30, 30);
		
		JButton newLevel = new JButton(createImageIcon("images/newicon.png"));
		newLevel.setToolTipText("New Level");
		newLevel.setActionCommand(EditorTool.NEW_LEVEL.toString());
		newLevel.setPreferredSize(buttonSize);
		newLevel.addActionListener(control);
		this.add(newLevel);
		
		JButton saveLevel = new JButton(createImageIcon("images/saveicon.png"));
		saveLevel.setToolTipText("Save Level");
		saveLevel.setActionCommand(EditorTool.SAVE_LEVEL.toString());
		saveLevel.setPreferredSize(buttonSize);
		saveLevel.addActionListener(control);
		this.add(saveLevel);
		
		JButton loadLevel = new JButton(createImageIcon("images/loadicon.png"));
		loadLevel.setToolTipText("Load Level");
		loadLevel.setActionCommand(EditorTool.LOAD_LEVEL.toString());
		loadLevel.setPreferredSize(buttonSize);
		loadLevel.addActionListener(control);
		this.add(loadLevel);
		
		JButton setPassDots = new JButton(createImageIcon("images/setdotsicon.png"));
		setPassDots.setToolTipText("Set Pass Dots");
		setPassDots.setActionCommand(EditorTool.SET_PASS_DOTS.toString());
		setPassDots.setPreferredSize(buttonSize);
		setPassDots.addActionListener(control);
		this.add(setPassDots);
		
		JButton addTile = new JButton(createImageIcon("images/addtileicon.png"));
		addTile.setToolTipText("Add Tile");
		addTile.setActionCommand(EditorTool.ADD_TILE.toString());
		addTile.setPreferredSize(buttonSize);
		addTile.addActionListener(control);
		this.add(addTile);
		
		JButton removeTile = new JButton(createImageIcon("images/removetileicon.png"));
		removeTile.setToolTipText("Remove Tile");
		removeTile.setActionCommand(EditorTool.REMOVE_TILE.toString());
		removeTile.setPreferredSize(buttonSize);
		removeTile.addActionListener(control);
		this.add(removeTile);
		
		JButton addWall = new JButton(createImageIcon("images/onewaywallicon.png"));
		addWall.setToolTipText("Add Wall (One Way)");
		addWall.setActionCommand(EditorTool.ADD_WALL.toString());
		addWall.setPreferredSize(buttonSize);
		addWall.addActionListener(control);
		this.add(addWall);
		
		JButton addExit = new JButton(createImageIcon("images/removeonewaywallicon.png"));
		addExit.setToolTipText("Remove Wall (One Way)");
		addExit.setActionCommand(EditorTool.CLEAR_WALL.toString());
		addExit.setPreferredSize(buttonSize);
		addExit.addActionListener(control);
		this.add(addExit);
		
		JButton addWallLinked = new JButton(createImageIcon("images/wallicon.png"));
		addWallLinked.setToolTipText("Add Wall");
		addWallLinked.setActionCommand(EditorTool.ADD_WALL_LINKED.toString());
		addWallLinked.setPreferredSize(buttonSize);
		addWallLinked.addActionListener(control);
		this.add(addWallLinked);
		
		JButton addExitLinked = new JButton(createImageIcon("images/removewallicon.png"));
		addExitLinked.setToolTipText("Remove Wall");
		addExitLinked.setActionCommand(EditorTool.CLEAR_WALL_LINKED.toString());
		addExitLinked.setPreferredSize(buttonSize);
		addExitLinked.addActionListener(control);
		this.add(addExitLinked);
		
		JButton addPortal = new JButton(createImageIcon("images/portalicon.png"));
		addPortal.setToolTipText("Add Portal");
		addPortal.setActionCommand(EditorTool.ADD_PORTAL.toString());
		addPortal.setPreferredSize(buttonSize);
		addPortal.addActionListener(control);
		this.add(addPortal);
		
		JButton addSpawn = new JButton(createImageIcon("images/spawnicon.png"));
		addSpawn.setToolTipText("Add Spawn");
		addSpawn.setActionCommand(EditorTool.ADD_SPAWN.toString());
		addSpawn.setPreferredSize(buttonSize);
		addSpawn.addActionListener(control);
		this.add(addSpawn);
		
		JButton clearSpawn = new JButton(createImageIcon("images/removespawnicon.png"));
		clearSpawn.setToolTipText("Remove Spawn");
		clearSpawn.setActionCommand(EditorTool.CLEAR_SPAWN.toString());
		clearSpawn.setPreferredSize(buttonSize);
		clearSpawn.addActionListener(control);
		this.add(clearSpawn);
	}
	
	/** Returns an ImageIcon, or null if the path was invalid. */
	private ImageIcon createImageIcon(String path) {
	    java.net.URL imgURL = getClass().getResource(path);
	    if (imgURL != null) {
	        return new ImageIcon(imgURL, "");
	    } else {
	        System.err.println("Couldn't find file: " + path);
	        return null;
	    }
	}
}
